package com.huawei.petapp2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.huawei.petapp2.R;
import com.huawei.petapp2.adapter.CommentListAdapter;
import com.huawei.petapp2.call.ProductCallBack;
import com.huawei.petapp2.constant.HttpUrl;
import com.huawei.petapp2.entity.CommentListBean;
import com.huawei.petapp2.entity.ProductComment;
import com.huawei.petapp2.entity.ProductVo;
import com.huawei.petapp2.entity.Result;
import com.huawei.petapp2.utils.DateUtil;
import com.huawei.petapp2.utils.PreferenceHelper;
import com.huawei.petapp2.view.DividerDecoration;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author: njb
 * @date: 2020/3/24 0024 10:48
 * @desc: 商品评论列表
 */
public class FMCommonList extends BaseFragment {
    private CommentListAdapter commentListAdapter;
    @BindView(R.id.rv_common_list)
    RecyclerView recyclerView;
    private List<CommentListBean> mList;

    final private static Gson gson=new GsonBuilder().serializeNulls().create();
    Context context;
    ProductVo storeitem;
    private List<ProductComment>productComments;
    private String productId;
    View view;
    public void lazyLoad() {
        mList = new ArrayList<>();
        List<String> list = new ArrayList<>();
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");

        for(int i=0;i<10;i++){
            CommentListBean commentListBean = new CommentListBean();
            commentListBean.setAddress("北京市昌平区立水桥");
            commentListBean.setContent("鞋子形状做的比较讨我喜欢，做工不错,鞋子形状做的比较讨我喜欢，做工不错,鞋子形状做的比较讨我喜欢鞋子形状做的比较讨我喜欢鞋子形状做的比较讨我喜欢");
            mList.add(commentListBean);
        }
        getProductComments(view,productId);
        /*OkHttpClient client = new OkHttpClient();
        int userid= PreferenceHelper.getInt("userId", Objects.requireNonNull(getContext()));
        String token=PreferenceHelper.getString("token",Objects.requireNonNull(getContext()));
        Request request = new Request.Builder()
                .url(HttpUrl.getBaseUrl()+"/product/detail-commonts/"+productId)//请求的url
                .addHeader("Token",token)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Looper.prepare();//增加部分
                if(response.code() >= 200 && response.code() < 300) {
                    String results = response.body().string();
                    Result result=gson.fromJson(results,Result.class);
                    System.out.println("新的商品列表请求OnintView11"+result+"\n");
                    productComments = DateUtil.getList13(result);
                    commentListAdapter = new CommentListAdapter(productComments);
                }
                Looper.loop();//增加部分
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                recyclerView.addItemDecoration(new DividerDecoration(context.getResources().getColor(R.color.ce7e7e7), 1));
                recyclerView.setAdapter(commentListAdapter);

            }
        });*/


    }



    @Override
    public void initView(View mView) {
        recyclerView=mView.findViewById(R.id.rv_common_list);
        this.context=mView.getContext();
        this.view=mView;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            this.storeitem= (ProductVo) arguments.getSerializable("storeitem");
            this.productId=storeitem.getProduct().getProductId();
            System.out.println("传输过来的用来展示评论的storeitem"+storeitem.getProduct().getProductName());
        }
    }

    public int getLayoutId() {
        return R.layout.fm_common_list;
    }


    protected void initView(Context context) {
        int userid= PreferenceHelper.getInt("userId", Objects.requireNonNull(getContext()));
        String token=PreferenceHelper.getString("token",Objects.requireNonNull(getContext()));
        this.context=context;
        mList = new ArrayList<>();
        List<String> list = new ArrayList<>();
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");

        for(int i=0;i<10;i++){
            CommentListBean commentListBean = new CommentListBean();
            commentListBean.setAddress("北京市昌平区立水桥");
            commentListBean.setContent("鞋子形状做的比较讨我喜欢，做工不错,鞋子形状做的比较讨我喜欢，做工不错,鞋子形状做的比较讨我喜欢鞋子形状做的比较讨我喜欢鞋子形状做的比较讨我喜欢");
            mList.add(commentListBean);
        }
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(HttpUrl.getBaseUrl()+"/product/detail-commonts/"+productId)//请求的url
                .addHeader("Token",token)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Looper.prepare();//增加部分
                if(response.code() >= 200 ) {
                    String results = response.body().string();
                    Result result=gson.fromJson(results,Result.class);
                    System.out.println("新的商品列表请求OnintView11"+result+"\n");
                    productComments = DateUtil.getList13(result);

                }
                Looper.loop();//增加部分
            }
        });
    }


    public List<ProductComment> getProductComments(View view,String productId)
    {
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        int userId= PreferenceHelper.getInt("userId", Objects.requireNonNull(getContext()));
        String token=PreferenceHelper.getString("token",Objects.requireNonNull(getContext()));

        String url = HttpUrl.getBaseUrl()+"/product/detail-commonts/"+productId;
        OkHttpUtils//
                .get()
                .url(url)//
//                .params(params)//
                .addHeader("Token",token)
                .build()//
                .execute(new ProductCallBack()//
                {


                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(List<ProductComment> response, int id) {
                       // int count=Integer.valueOf(response.getCartNum()).intValue();
                        productComments =response;
                       System.out.println("当前购评论商品id"+productComments.get(0).getCommContent());
                        // int count = CartImpl.getCartSize();
                        commentListAdapter = new CommentListAdapter(productComments);
                        recyclerView.setLayoutManager(new LinearLayoutManager(context));
                        recyclerView.addItemDecoration(new DividerDecoration(context.getResources().getColor(R.color.ce7e7e7), 1));
                        recyclerView.setAdapter(commentListAdapter);
                    }

                });
        return productComments;

    }
}
